import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import { hasPermission } from './plugin/permission'

// import 'font-awesome/css/font-awesome.min.css' // replace as cdn resource

import {
  Pagination,
  Dialog,
  // Autocomplete,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  Menu,
  Submenu,
  MenuItem,
  MenuItemGroup,
  Input,
  InputNumber,
  Radio,
  RadioGroup,
  RadioButton,
  Checkbox,
  CheckboxButton,
  CheckboxGroup,
  // Switch,
  Select,
  Option,
  OptionGroup,
  Button,
  ButtonGroup,
  Table,
  TableColumn,
  DatePicker,
  TimeSelect,
  TimePicker,
  Popover,
  Tooltip,
  Breadcrumb,
  BreadcrumbItem,
  Form,
  FormItem,
  Tabs,
  TabPane,
  Tag,
  Tree,
  // Alert,
  // Slider,
  Icon,
  Row,
  Col,
  Upload,
  Progress,
  // Badge,
  Card,
  // Rate,
  // Steps,
  // Step,
  // Carousel,
  // CarouselItem,
  // Collapse,
  // CollapseItem,
  Cascader,
  // ColorPicker,
  // Transfer,
  // Container,
  // Header,
  // Aside,
  // Main,
  // Footer,
  Loading,
  MessageBox,
  Message,
  Notification
} from 'element-ui'

Vue.use(Pagination)
Vue.use(Dialog)
// Vue.use(Autocomplete)
Vue.use(Dropdown)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(MenuItemGroup)
Vue.use(Input)
Vue.use(InputNumber)
Vue.use(Radio)
Vue.use(RadioGroup)
Vue.use(RadioButton)
Vue.use(Checkbox)
Vue.use(CheckboxButton)
Vue.use(CheckboxGroup)
// Vue.use(Switch)
Vue.use(Select)
Vue.use(Option)
Vue.use(OptionGroup)
Vue.use(Button)
Vue.use(ButtonGroup)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(DatePicker)
Vue.use(TimeSelect)
Vue.use(TimePicker)
Vue.use(Popover)
Vue.use(Tooltip)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Tabs)
Vue.use(TabPane)
Vue.use(Tag)
Vue.use(Tree)
// Vue.use(Alert)
// Vue.use(Slider)
Vue.use(Icon)
Vue.use(Row)
Vue.use(Col)
Vue.use(Upload)
Vue.use(Progress)
// Vue.use(Badge)
Vue.use(Card)
// Vue.use(Rate)
// Vue.use(Steps)
// Vue.use(Step)
// Vue.use(Carousel)
// Vue.use(CarouselItem)
// Vue.use(Collapse)
// Vue.use(CollapseItem)
Vue.use(Cascader)
// Vue.use(ColorPicker)
// Vue.use(Container)
// Vue.use(Header)
// Vue.use(Aside)
// Vue.use(Main)
// Vue.use(Footer)

Vue.use(Loading.directive)

const myplugins = [
  hasPermission
]

myplugins.map((plugin) => {
  Vue.use(plugin)
})

Vue.prototype.$loading = Loading.service
Vue.prototype.$msgbox = MessageBox
Vue.prototype.$alert = MessageBox.alert
Vue.prototype.$confirm = MessageBox.confirm
// Vue.prototype.$prompt = MessageBox.prompt
Vue.prototype.$notify = Notification
Vue.prototype.$message = Message
Vue.config.productionTip = false

store.commit('INIT_STATE', false) // Init Application with default state

router.beforeEach((to, from, next) => {
  store.commit('SET_LOADING', true)
  let menus = store.getters.userMenus
  if (!menus) { // 判断当前用户是否已拉取完user_info信息
    // get user's info and menus
    store.dispatch('USER_INFO').then(info => { // 拉取user_info
      // console.log('main js userinfo ok, and store menus: ')
      let menuCode = store.getters.userMenus
      store.dispatch('GENERATE_ASYNC_ROUTES', menuCode).then(info => {
        let add = store.getters.addedRouters
        if (add) {
          router.addRoutes(add) // 动态添加 可访问的路由列表
        }
        // console.log(router)
      })
      if (to.path === '/login') {
        next('/')
      } else {
        next()
      }
    }).catch(error => {
      store.dispatch('DEL_LOGIN_TOKEN')
      console.log(error)
      if (to.path !== '/login') {
        next({
          path: '/login',
          query: { redirect: to.fullPath }
        })
      } else {
        next()
      }
    })
  } else {
    next() // 有 token 有menus
  }
})

router.afterEach(transition => {
  if (!store.getters.getLoading) {
    return
  }
  setTimeout(() => {
    store.commit('SET_LOADING', false)
  }, 500)
})

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
